import router from './router'
import store from './store'
import Cookie from 'js-cookie'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'
import { TokenType } from '@/utils/enum'
import { Loading } from 'element-ui';
import Token from './utils/token'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login','/wxWorkLogin','/qrcode-login'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()
  // set page title
  document.title = getPageTitle(to.meta.title)
  var user_token = Cookie.get(TokenType.USER_TOKEN)
  var user_refreshtoken = Cookie.get(TokenType.USER_REFRESHTOKEN)
  console.log(to.path)

  if(to.path == '/logout') {
    Token.remove();
    sessionStorage.clear();
    return
  }
  //有token 登录直接跳转到首页
  if (to.path.indexOf('index') < 0 && to.path.indexOf('news') < 0) {
    console.log("非法路由跳转到首页")
    next({ path: '/index' })
    return
  }
  let isPhone = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
  console.log("to", to)
  if(isPhone && to.path.indexOf("_m") < 0) {
    var path = to.path + "_m"
    var newTo = {...to, path: path}
    console.log("newTo", newTo)
    next(newTo)
  }
  next()
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
